﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using nkstdgia.common.ThamDinhGia.BusinessLogic;
using nkstdgia.common.ThamDinhGia.BusinessObjects;
using wsRK.Web;
using wsRK.wsControls;

namespace nkstdgia.main
{
    public partial class quanhuyen : WebPage
    {
        #region Variable

        #endregion Variable

        #region Properties

        public List<THAM_SO> LstThamSo
        {
            get
            {
                return ViewState["LstThamSo"] == null ? null : ViewState["LstThamSo"] as List<THAM_SO>;
            }
            set { ViewState["LstThamSo"] = value; }
        }

        #endregion Properties

        #region Construction

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this._createData();
                this.lvQuan.SelectedIndex = 0;
                this._getDataQuan(-1);
                this._getDataPhuong();
            }
        }

        #endregion Construction

        #region Get Data

        private void _createData()
        {
            this.LstThamSo = ThamDinhGiaFacade.GetTHAM_SOBL().GetList();
            this.LstThamSo = (from t in this.LstThamSo
                              where t.LOAI_THAM_SO == "LOAI_QUAN" || t.LOAI_THAM_SO == "LOAI_PHUONG"
                              select t).ToList();
        }

        private void _getDataQuan(int intSelectedQuanID)
        {
            List<THAM_SO> lsThamSo = (from t in this.LstThamSo
                                      where t.LOAI_THAM_SO == "LOAI_QUAN"
                                      orderby t.SEQUENCE ascending
                                      select t).ToList();
            if (intSelectedQuanID >= 0 && lsThamSo.Count > 0)
            {
                THAM_SO ts = (from t in lsThamSo
                              where t.MA_THAM_SO == intSelectedQuanID
                              select t).FirstOrDefault();
                if (ts != null)
                {
                    int i = lsThamSo.IndexOf(ts);
                    this.lvQuan.SelectedIndex = i;
                    this.lvQuan.DataSource = lsThamSo;
                    this.lvQuan.DataBind();
                    this._getDataPhuong();
                }
                else
                {
                    this.lvQuan.SelectedIndex = 0;
                    this.lvQuan.DataSource = lsThamSo;
                    this.lvQuan.DataBind();
                    this._getDataPhuong();
                }
            }
            else
            {
                this.lvQuan.DataSource = lsThamSo;
                this.lvQuan.DataBind();
            }
        }

        private void _getDataPhuong()
        {
            if (lvQuan.SelectedIndex < 0)
            {
                lvQuan.SelectedIndex = 0;
            }
            if (lvQuan.Items.Count > 0)
            {
                ListViewDataItem item = lvQuan.Items[lvQuan.SelectedIndex];
                HiddenField hfID = item.FindControl("hfID") as HiddenField;
                int intMaThamSoQuan = int.Parse(hfID.Value);
                this._getDataPhuong(intMaThamSoQuan);
            }
        }

        private void _getDataPhuong(int intMaThamSoQuan)
        {
            List<THAM_SO> lsThamSo = (from t in this.LstThamSo
                                      where t.LOAI_THAM_SO == "LOAI_PHUONG" && t.PARENT_ID == intMaThamSoQuan
                                      orderby t.SEQUENCE ascending
                                      select t).ToList();
            this.lvPhuong.DataSource = lsThamSo;
            this.lvPhuong.DataBind();
        }

        #endregion Get Data

        #region Event

        protected void lvQuan_ItemCommand(object sender, System.Web.UI.WebControls.ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int intMaThamSo = int.Parse(e.CommandArgument.ToString());
                this._getDataPhuong(intMaThamSo);
            }
            else if (e.CommandName == "DeleteQuan")
            {
                try
                {
                    ListViewDataItem item = lvQuan.Items[lvQuan.SelectedIndex];
                    HiddenField hfID = item.FindControl("hfID") as HiddenField;
                    int intSelectedQuanID = int.Parse(hfID.Value);

                    int intMaThamSo = int.Parse(e.CommandArgument.ToString());
                    ThamDinhGiaFacade.GetTHAM_SOBL().Delete((long)intMaThamSo);
                    var ts = (from t in this.LstThamSo
                              where t.MA_THAM_SO == intMaThamSo
                              select t).FirstOrDefault();
                    if (ts != null)
                    {
                        this.LstThamSo.Remove(ts);
                    }
                    this._getDataQuan(intSelectedQuanID);

                    base.showMasterMessage("Xóa thành công");
                }
                catch (Exception)
                {
                    base.showMessage(true, "Đóng", "Dữ liệu đang sử dụng");
                    //base.showMasterMessage("Dữ liệu đang sử dụng");
                }
            }
        }

        protected void lvQuan_SelectedIndexChanging(object sender, System.Web.UI.WebControls.ListViewSelectEventArgs e)
        {
            this.lvQuan.SelectedIndex = e.NewSelectedIndex;
            this._getDataQuan(-1);
        }

        protected void lvQuan_ItemEditing(object sender, System.Web.UI.WebControls.ListViewEditEventArgs e)
        {
            this.lvQuan.EditIndex = e.NewEditIndex;
            this._getDataQuan(-1);
        }

        protected void lvQuan_ItemUpdating(object sender, System.Web.UI.WebControls.ListViewUpdateEventArgs e)
        {
            ListViewDataItem item = lvQuan.Items[e.ItemIndex];
            TextBox txtTenQuan = item.FindControl("txtTenQuan") as TextBox;
            wsTxtInterger txtThuTu = item.FindControl("txtThuTu") as wsTxtInterger;
            HiddenField hfID = item.FindControl("hfID") as HiddenField;
            if (txtTenQuan != null && txtThuTu != null && hfID != null)
            {
                int key = int.Parse(hfID.Value);
                //THAM_SO thamso = ThamDinhGiaFacade.GetTHAM_SOBL().GetByMA_THAM_SO(key);
                THAM_SO thamso = (from t in this.LstThamSo
                                  where t.MA_THAM_SO == key
                                  select t).FirstOrDefault();
                if (thamso != null)
                {
                    thamso.NOI_DUNG = txtTenQuan.Text;
                    thamso.SEQUENCE = txtThuTu.Value;

                    ThamDinhGiaFacade.GetTHAM_SOBL().Update(thamso);

                    this.lvQuan.EditIndex = -1;
                    this._getDataQuan(-1);

                    string msg = "Cập nhật thành công";
                    base.showMasterMessage(msg);
                }
            }
        }

        protected void lvQuan_ItemCanceling(object sender, System.Web.UI.WebControls.ListViewCancelEventArgs e)
        {
            lvQuan.EditIndex = -1;
            this._getDataQuan(-1);
        }

        protected void lvPhuong_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            lvPhuong.EditIndex = -1;
            this._getDataPhuong();
        }

        protected void lvPhuong_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            { }
            else if (e.CommandName == "DeletePhuong")
            {
                try
                {
                    int intMaThamSo = int.Parse(e.CommandArgument.ToString());
                    ThamDinhGiaFacade.GetTHAM_SOBL().Delete((long)intMaThamSo);
                    var ts = (from t in this.LstThamSo
                              where t.MA_THAM_SO == intMaThamSo
                              select t).FirstOrDefault();
                    if (ts != null)
                    {
                        this.LstThamSo.Remove(ts);
                    }
                    this._getDataPhuong();
                    base.showMasterMessage("Xóa thành công");
                }
                catch (Exception)
                {
                    base.showMessage(true, "Đóng", "Dữ liệu đang sử dụng");
                    //base.showMasterMessage("Dữ liệu đang sử dụng");
                }
            }
        }

        protected void lvPhuong_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            this.lvPhuong.EditIndex = e.NewEditIndex;
            this._getDataPhuong();
        }

        protected void lvPhuong_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            ListViewDataItem item = lvPhuong.Items[e.ItemIndex];
            TextBox txtTenPhuong = item.FindControl("txtTenPhuong") as TextBox;
            wsTxtInterger txtThuTu = item.FindControl("txtThuTu") as wsTxtInterger;
            HiddenField hfID = item.FindControl("hfID") as HiddenField;
            if (txtTenPhuong != null && txtThuTu != null && hfID != null)
            {
                int key = int.Parse(hfID.Value);
                //THAM_SO thamso = ThamDinhGiaFacade.GetTHAM_SOBL().GetByMA_THAM_SO(key);
                THAM_SO thamso = (from t in this.LstThamSo
                                  where t.MA_THAM_SO == key
                                  select t).FirstOrDefault();
                if (thamso != null)
                {
                    thamso.NOI_DUNG = txtTenPhuong.Text;
                    thamso.SEQUENCE = txtThuTu.Value;

                    ThamDinhGiaFacade.GetTHAM_SOBL().Update(thamso);

                    this.lvQuan.EditIndex = -1;
                    this._getDataPhuong();

                    string msg = "Cập nhật thành công";
                    base.showMasterMessage(msg);
                }
            }
        }

        protected void lvPhuong_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
        {
            this.lvPhuong.SelectedIndex = e.NewSelectedIndex;
        }

        protected void btnAddQuanPopup_Click(object sender, EventArgs e)
        {
            THAM_SO thamso = new THAM_SO()
            {
                LOAI_THAM_SO = "LOAI_QUAN",
                NOI_DUNG = txtTenQuanPopup.Text,
                SEQUENCE = txtThuTuQuan.Value,
                PARENT_ID = 0,
                MA_THAM_SO = 0,
                GHI_CHU = ""
            };
            long NewID = ThamDinhGiaFacade.GetTHAM_SOBL().Add(thamso);
            if (NewID >= 0)
            {
                txtTenQuanPopup.Text = "";
                txtThuTuQuan.Text = "0";

                this._createData();

                this._getDataQuan(-1);
                base.showMasterMessage("Thêm mới thành công");
            }
            else
            {
                base.showMasterMessage("Thêm mới thất bại");
            }
        }

        protected void btnAddPhuongPopup_Click(object sender, EventArgs e)
        {
            ListViewDataItem item = lvQuan.Items[lvQuan.SelectedIndex];
            HiddenField hfID = item.FindControl("hfID") as HiddenField;

            THAM_SO thamso = new THAM_SO()
            {
                LOAI_THAM_SO = "LOAI_PHUONG",
                NOI_DUNG = txtTenPhuong.Text,
                SEQUENCE = txtThuTuPhuong.Value,
                PARENT_ID = int.Parse(hfID.Value),
                GHI_CHU = ""
            };
            long NewID = ThamDinhGiaFacade.GetTHAM_SOBL().Add(thamso);
            if (NewID >= 0)
            {
                txtTenPhuong.Text = "";
                txtThuTuPhuong.Text = "0";

                this._createData();

                int intMaThamSo = int.Parse(hfID.Value);
                this._getDataPhuong(intMaThamSo);
                base.showMasterMessage("Thêm mới thành công");
            }
            else
            {
                base.showMasterMessage("Thêm mới thất bại");
            }
        }

        #endregion Event
    }
}